# -*- coding: utf-8 -*- 
# @project : 《Atcoder》
# @Author : created by bensonrachel on 2021/8/26
# @File : TPM（step2）G. Coprime Segment（st表）.py


#st表做法，与上一题完全一样，建表时把求最值改成求GCD。
import math
def create_log_table():
    table = [0]*(n+1)
    table[1]=0
    for i in range(2,n+1):
        table[i] = table[i//2]+1
    return table
def create_gcd_st():
    for i in range(n):
        gcd_table[i][0]=rate[i]
    for j in range(1,Log_table[n]+1):
        i = 0
        while(i+2**j-1<=n-1):
            gcd_table[i][j] = math.gcd(gcd_table[i][j-1],gcd_table[i+2**(j-1)][j-1])
            i +=1
def query(l,r):
    length = r-l+1
    x = Log_table[length]
    return math.gcd(gcd_table[l][x],gcd_table[r-2**x+1][x])
def TPM():
    l = 0
    res = n+1
    for r in range(n):
        while(l <= r and query(l,r)==1):#这里不能while(query(l,r)>k):，l=r时有可能这个数就是1
            res = min(res,r-l+1)
            l += 1
    return res
"""
GCD题：
st表做法，时间复杂度级别（nlogn）
"""
if __name__ == "__main__":
    n = int(input())
    rate = [int(i) for i in input().split()]
    Log_table = create_log_table()
    gcd_table = [[0]*(18) for i in range(n)]
    create_gcd_st()
    x = TPM()
    if(x==n+1):
        x = -1
    print(x)